<Project Sdk="Microsoft.NET.Sdk" InitialTargets="CreateProjectWithColonInSourceName">
  <PropertyGroup>
    <TargetFramework>$(AspNetCoreAppCurrent)</TargetFramework>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <IsTestProject>true</IsTestProject>
    <RunAnalyzers>false</RunAnalyzers>
    <DebuggerHost Condition="'$(DebuggerHost)' == '' or ('$(DebuggerHost)' != 'chrome' and '$(DebuggerHost)' != 'firefox')">chrome</DebuggerHost>
    <DefineConstants Condition="'$(DebuggerHost)' == 'chrome'">$(DefineConstants);RUN_IN_CHROME</DefineConstants>
    <DefineConstants Condition="'$(RuntimeConfiguration)' == 'release'">$(DefineConstants);RELEASE_RUNTIME</DefineConstants>
    <BrowserHost Condition="$([MSBuild]::IsOSPlatform('windows'))">windows</BrowserHost>
    <!-- '/.dockerenv' - is to check if this is running in a codespace -->
    <_ProvisionBrowser Condition="'$(ContinuousIntegrationBuild)' == 'true' or Exists('/.dockerenv')">true</_ProvisionBrowser>
    <InstallChromeForTests Condition="'$(InstallChromeForTests)' == '' and '$(DebuggerHost)' == 'chrome' and '$(_ProvisionBrowser)' == 'true'">true</InstallChromeForTests>
    <InstallFirefoxForTests Condition="'$(InstallFirefoxForTests)' == '' and '$(DebuggerHost)' == 'firefox' and '$(_ProvisionBrowser)' == 'true'">true</InstallFirefoxForTests>

    <!-- Set RunSettingsFilePath property which is read by VSTest. -->
    <RunSettingsFilePath Condition="'$(ContinuousIntegrationBuild)' == 'true'">$(OutputPath).runsettings</RunSettingsFilePath>
    <RunSettingsFilePath Condition="'$(ContinuousIntegrationBuild)' != 'true'">$(ArtifactsObjDir)vscode\.runsettings</RunSettingsFilePath>

    <!-- Serilog.Extensions.Logging.File brings in NS1.x dependencies -->
    <FlagNetStandard1XDependencies>false</FlagNetStandard1XDependencies>
  </PropertyGroup>

  <Import Project="$(RepositoryEngineeringDir)testing\wasm-provisioning.targets" />

  <ItemGroup>
    <Content Include="appsettings.json" CopyToOutputDirectory="PreserveNewest" />

    <Compile Include="..\BrowserDebugProxy\Common\*.cs" />

    <!-- TODO [ActiveIssue("https://github.com/dotnet/runtime/issues/85168")] 
    <Compile Remove="EvaluateOnCallFrameTests.cs" />
    -->

    <Compile Include="$(LibrariesProjectRoot)Microsoft.Extensions.Logging\tests\DI.Common\Common\src\XunitLoggerFactoryExtensions.cs" />
    <Compile Include="$(LibrariesProjectRoot)Microsoft.Extensions.Logging\tests\DI.Common\Common\src\XunitLoggerProvider.cs" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\BrowserDebugHost\BrowserDebugHost.csproj" />
    <ProjectReference Include="..\BrowserDebugProxy\BrowserDebugProxy.csproj" />

    <!--
        - This project needs to use `$(AspNetCoreAppCurrent)` as it uses aspnetcore, and that
          version can lag behind `$(NetCoreAppCurrent)`.
        - `debugger-test.csproj` builds the test *apps* for `$(NetCoreAppCurrent)`, is not
          directly referenced from the test assembly.
          - so, the `ProjectReference` has `ReferenceOutputAssembly=false`
          - it also has SkipGetTargetFrameworkProperties=true, which essentially
            skips the target that would try to find a tfm for debugger-test.csproj
            compatible with this project.
          - But that would clash when $(AspNetCoreAppCurrent) != $(NetCoreAppCurrent), so skipping the
            target ensures that the correct tfm is used.
    -->
    <ProjectReference Include="..\tests\debugger-test\debugger-test.csproj"
                      AdditionalProperties="RuntimeConfiguration=$(RuntimeConfiguration);TasksConfiguration=$(RuntimeConfiguration)"
                      ReferenceOutputAssembly="false"
                      Private="false"
                      SkipGetTargetFrameworkProperties="true"
                      />

    <PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0" />
    <!-- needed for ConditionalFact etc -->
    <ProjectReference Include="$(RepoRoot)src\libraries\Common\tests\TestUtilities\TestUtilities.csproj" />
  </ItemGroup>

  <Target Name="CopyTestZipForHelix"
          Condition="'$(ArchiveDirForHelix)' != ''"
          AfterTargets="Build">

    <ItemGroup>
      <_FilesToCopy Include="$(OutputPath)\**\*" TargetPath="DebuggerTestSuite" />
      <_FilesToCopy Include="$(ArtifactsBinDir)debugger-test\Debug\**\*" TargetPath="debugger-test" />
    </ItemGroup>

    <Copy SourceFiles="@(_FilesToCopy)" DestinationFiles="$(ArchiveDirForHelix)\%(TargetPath)\%(RecursiveDir)%(FileName)%(Extension)" />
  </Target>

  <Target Name="_ExtractTestClassNames"
          Condition="'$(ExtractTestClassNamesForHelix)' == 'true'"
          BeforeTargets="CopyTestZipForHelix"
          DependsOnTargets="_GenerateRunSettingsFile">

    <Exec Command="&quot;$(DotNetTool)&quot; test --no-build -s $(RunSettingsFilePath) -t --nologo -v:q -p:VsTestUseMSBuildOutput=false" ConsoleToMSBuild="true">
      <Output TaskParameter="ConsoleOutput" ItemName="_ListOfTestsLines" />
    </Exec>

    <PropertyGroup>
      <_Regex>^ *(DebuggerTests[^\($]+)</_Regex>
    </PropertyGroup>
    <ItemGroup>
      <_TestLines0 Include="$([System.Text.RegularExpressions.Regex]::Match('%(_ListOfTestsLines.Identity)', '$(_Regex)'))" />
      <TestClassName Include="$([System.IO.Path]::GetFileNameWithoutExtension('%(_TestLines0.Identity)'))" />
    </ItemGroup>

    <Error Text="No DebuggerTests test classes found!" Condition="'@(TestClassName)' == ''" />

    <WriteLinesToFile File="$(TestArchiveTestsDir)$(MSBuildProjectName).tests.list"
                      Lines="@(TestClassName->Distinct())"
                      Overwrite="true" />
  </Target>

  <!-- Copy of `GenerateRunSettingsFile` from eng/testing/runsettings.targets -->
  <Target Name="_GenerateRunSettingsFile" AfterTargets="Build" BeforeTargets="Test">
    <Message Text="*** Generating .runsettings file in $(RunSettingsFilePath)" Importance="High" />
    <PropertyGroup>
      <DotNetForTests Condition="'$(DotNetForTests)' == '' and  $([System.IO.Path]::IsPathRooted($(DotNetTool)))">$(DotNetTool)</DotNetForTests>
      <DotNetForTests Condition="'$(DotNetForTests)' == ''">$([System.IO.Path]::Combine($(DotNetRoot), $(DotNetTool)))</DotNetForTests>

      <_RunSettingsInputFilePath>$(RepositoryEngineeringDir)testing\.runsettings</_RunSettingsInputFilePath>
      <RunSettingsFileContent>$([System.IO.File]::ReadAllText('$(_RunSettingsInputFilePath)'))</RunSettingsFileContent>
      <RunSettingsFileContent Condition="'$(TestDisableParallelization)' == 'true'">$(RunSettingsFileContent.Replace('$$MAXCPUCOUNT$$', '1'))</RunSettingsFileContent>
      <RunSettingsFileContent Condition="'$(TestDisableParallelization)' != 'true'">$(RunSettingsFileContent.Replace('$$MAXCPUCOUNT$$', '0'))</RunSettingsFileContent>
      <RunSettingsFileContent>$(RunSettingsFileContent.Replace('$$TARGETPLATFORM$$', ''))</RunSettingsFileContent>
      <RunSettingsFileContent>$(RunSettingsFileContent.Replace('$$COVERAGE_INCLUDE$$', '$(CoverageIncludeFilter)')
                                                      .Replace('$$COVERAGE_EXCLUDEBYFILE$$', '$(CoverageExcludeByFileFilter)')
                                                      .Replace('$$COVERAGE_INCLUDEDIRECTORY$$', '$(CoverageIncludeDirectoryFilter)')
                                                      .Replace('$$COVERAGE_ENABLED$$', '$([MSBuild]::ValueOrDefault('$(Coverage)', 'false'))')
                                                      .Replace('$$DISABLEPARALLELIZATION$$', '$([MSBuild]::ValueOrDefault('$(TestDisableParallelization)', 'false'))')
                                                      .Replace('$$DISABLEAPPDOMAIN$$', '$([MSBuild]::ValueOrDefault('$(TestDisableAppDomain)', 'false'))')
                                                      .Replace('$$TESTCASEFILTER$$', '$(_testFilter)')
                                                      .Replace('$$DOTNETHOSTPATH$$', '$(DotNetForTests)'))</RunSettingsFileContent>
    </PropertyGroup>

    <WriteLinesToFile File="$(RunSettingsFilePath)"
                      Lines="$(RunSettingsFileContent)"
                      WriteOnlyWhenDifferent="true"
                      Overwrite="true" />
  </Target>

  <Target Name="CreateProjectWithColonInSourceName"
    Condition="!$([MSBuild]::IsOSPlatform('windows'))">
    <PropertyGroup>
        <CsContent>
            namespace DebuggerTests
            {
                public class CheckColonInSourceName
                {
                    public static void Evaluate()
                    {
                        var a = 123%3B
                    }
                }
            }
        </CsContent>
    </PropertyGroup>
    <WriteLinesToFile
        File="../tests/debugger-test-with-colon-in-source-name/test:.cs"
        Lines="$(CsContent)"
        Overwrite="true"/>
  </Target>

</Project>
